// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
#include "commons.h"
#include "mdflush.h"
#include "mdflush.skel.h"
#include "trace_helpers.h"

static volatile sig_atomic_t exiting;
static volatile bool verbose = false;

const char *argp_program_version = "mdflush 0.1";
const char *argp_program_bug_address = "Jackie Liu <liuyun01@kylinos.cn>";
const char argp_program_doc[] =
    "Trace md flush events.\n"
    "\n"
    "USAGE: mdflush\n";

static const struct argp_option opts[] = {
    {"verbose", 'v', NULL, 0, "Verbose debug output"},
    {NULL, 'h', NULL, OPTION_HIDDEN, "Show the full help"},
    {}};

static error_t parse_arg(int key, char *arg, struct argp_state *state)
{
    switch (key)
    {
    case 'v':
        verbose = true;
        break;
    case 'h':
        argp_state_help(state, stderr, ARGP_HELP_STD_HELP);
        break;
    default:
        return ARGP_ERR_UNKNOWN;
    }
    return 0;
}

static void sig_handler(int sig)
{
    exiting = 1;
}

static int libbpf_print_fn(enum libbpf_print_level level, const char *format,
                           va_list args)
{
    if (level == LIBBPF_DEBUG && !verbose)
        return 0;
    return vfprintf(stderr, format, args);
}

static void handle_event(void *ctx, int cpu, void *data, __u32 data_sz)
{
    struct event *e = data;
    char ts[32];

    strftime_now(ts, sizeof(ts), "%H%M:%S");
    printf("%-8s %-7d %-16s %-s\n", ts, e->pid, e->comm, e->disk);
}

static void handle_lost_events(void *ctx, int cpu, __u64 lost_cnt)
{
    warning("Lost %llu events on CPU #%d\n", lost_cnt, cpu);
}

int main(int argc, char *argv[])
{
    static const struct argp argp = {
        .options = opts,
        .parser = parse_arg,
        .doc = argp_program_doc,
    };
    struct perf_buffer *pb = NULL;
    struct mdflush_bpf *obj;
    int err;

    err = argp_parse(&argp, argc, argv, 0, NULL, NULL);
    if (err)
        return err;

    libbpf_set_print(libbpf_print_fn);

    obj = mdflush_bpf__open();
    if (!obj)
    {
        warning("Failed to open BPF object\n");
        return 1;
    }

    if (fentry_can_attach("md_flush_request", NULL))
        bpf_program__set_autoload(obj->progs.kprobe_md_flush_request, false);
    else
        bpf_program__set_autoload(obj->progs.md_flush_request, false);

    err = mdflush_bpf__load(obj);
    if (err)
    {
        warning("Failed to load BPF object: %d\n", err);
        goto cleanup;
    }

    err = mdflush_bpf__attach(obj);
    if (err)
    {
        warning("Failed to attach BPF programs\n");
        goto cleanup;
    }

    pb = perf_buffer__new(bpf_map__fd(obj->maps.events), PERF_BUFFER_PAGES,
                          handle_event, handle_lost_events, NULL, NULL);
    if (!pb)
    {
        warning("Failed to open perf buffer: %d\n", err);
        goto cleanup;
    }

    if (signal(SIGINT, sig_handler) == SIG_ERR)
    {
        warning("Can't set signal handler: %s\n", strerror(errno));
        err = 1;
        goto cleanup;
    }

    printf("Tracing md flush requests... Hit Ctrl-C to end.\n");
    printf("%-8s %-7s %-16s %-s\n",
           "TIME", "PID", "COMM", "DEVICE");

    while (!exiting)
    {
        err = perf_buffer__poll(pb, PERF_POLL_TIMEOUT_MS);
        if (err < 0 && err != -EINTR)
        {
            warning("Error polling perf buffer: %s\n", strerror(-err));
            goto cleanup;
        }

        /* reset err to return 0 if exiting */
        err = 0;
    }

cleanup:
    perf_buffer__free(pb);
    mdflush_bpf__destroy(obj);

    return err != 0;
}
